package com.tools.elk;

import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class InsertBatchES {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        BulkRequest bulkRequest = new BulkRequest();

        bulkRequest.add(new IndexRequest().index("user").id("10001").source(XContentType.JSON,"name","王五"));
        bulkRequest.add(new IndexRequest().index("user").id("10002").source(XContentType.JSON,"name","阿萨大胜多负少大"));
        bulkRequest.add(new IndexRequest().index("user").id("10003").source(XContentType.JSON,"name","洞sd拐洞拐"));
        bulkRequest.add(new IndexRequest().index("user").id("10004").source(XContentType.JSON,"name","洞拐洞多个拐"));
        bulkRequest.add(new IndexRequest().index("user").id("10005").source(XContentType.JSON,"name","洞拐是否洞sf拐"));
        bulkRequest.add(new IndexRequest().index("user").id("10006").source(XContentType.JSON,"name","洞s拐洞拐"));
        bulkRequest.add(new IndexRequest().index("user").id("10007").source(XContentType.JSON,"name","洞多个拐洞多个拐"));
        bulkRequest.add(new IndexRequest().index("user").id("10008").source(XContentType.JSON,"name","洞拐给洞拐"));
        bulkRequest.add(new IndexRequest().index("user").id("10008").source(XContentType.JSON,"name","洞拐洞拐"));
        bulkRequest.add(new IndexRequest().index("user").id("10009").source(XContentType.JSON,"name","洞拐洞是否多东莞懂个拐"));
        bulkRequest.add(new IndexRequest().index("user").id("10010").source(XContentType.JSON,"name","洞拐洞释放锁的风格拐"));
        bulkRequest.add(new IndexRequest().index("user").id("10011").source(XContentType.JSON,"name","洞拐的风格洞拐"));
        bulkRequest.add(new IndexRequest().index("user").id("10012").source(XContentType.JSON,"name","洞拐洞多个拐"));

        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulk.getTook());
        System.out.println(bulk.getItems());

        client.close();


    }
}
